package com.vvlive.caibao.smartcity.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.utils.ScreenUtil;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/6/26.
 *
 *
 * +-------------------------+-----------------------+
 * |          Method         |      Details          |
 * +-------------------------+-----------------------+
 * |   getCommonActionBar()  | 获得ActionBar          |
 * +-------------------------+-----------------------+
 * |   setOnBackListener(l)  | 设置左上角的点击事件     |
 * +-------------------------+-----------------------+
 * |     setBackIcon(int id) | 设置左上角图标          |
 * +-------------------------+-----------------------+
 * |    setBackView(View v)  | 设置左上角控件          |
 * +-------------------------+-----------------------+
 * |   setBackVisible(false) | 设置左上角图标不可见     |
 * +-------------------------+-----------------------+
 * |   setOnMoreListener(l)  | 设置右上角的点击事件     |
 * +-------------------------+-----------------------+
 * |     setMoreIcon(int id) | 设置右上角图标          |
 * +-------------------------+-----------------------+
 * |    setMoreView(View v)  | 设置右上角控件          |
 * +-------------------------+-----------------------+
 * |   setMoreVisible(false) | 设置右上角图标不可见     |
 * +-------------------------+-----------------------+
 * |  setTitle(String title) | 设置标题               |
 * +-------------------------+-----------------------+
 * |  setTitleView(View view)| 设置标题控件            |
 * +-------------------------+-----------------------+
 * | setTitleVisible(false)  | 设置标题不可见          |
 * +-------------------------+-----------------------+
 * |     getBackView()       | 获得左上角控件          |
 * +-------------------------+-----------------------+
 * |     getTitleView()      | 获得标题控件            |
 * +-------------------------+-----------------------+
 * |      getMoreView()      | 获得右上角控件          |
 * +-------------------------+-----------------------+
 * |setBackgroundColor(int c)| 设置背景颜色            |
 * +-------------------------+-----------------------+
 *
 *
 */
public class CommonActionBar extends LinearLayout implements View.OnClickListener {

    /**
     * The height of actionbar, unchanged
     */
    public static final int ACTIONBAR_HEIGHT = 48;
    private final int DEFAULT_TEXT_SIZE = 20;
    private final int DEFAULT_RED = 254;
    private final int DEFAULT_GREEN = 102;
    private final int DEFAULT_BLUE = 97;

    private LinearLayout mLayout;
    private ImageButton mBack;
    private TextView mTitle;
    private ImageButton mMore;

    /**
     * 左上角按钮监听
     */
    private OnBackListener onBackListener;
    /**
     * 右上角按钮监听
     */
    private OnMoreListener onMoreListener;


    public CommonActionBar(Context context) {
        this(context, null);
    }

    public CommonActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout();
    }

    /**
     * 初始化布局
     */
    private void initLayout() {
        mLayout = (LinearLayout) ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.common_actionbar, null);
        mLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) new ScreenUtil(getContext()).dip2px(ACTIONBAR_HEIGHT)));
        mBack = (ImageButton) mLayout.findViewById(R.id.actionbar_back);
        mBack.setOnClickListener(this);
        mMore = (ImageButton) mLayout.findViewById(R.id.actionbar_more);
        mMore.setOnClickListener(this);
        mTitle = (TextView) mLayout.findViewById(R.id.actionbar_title);
        this.addView(mLayout);
    }

    /**
     * Set the background-color of the actionbar.
     *
     * @param color The color
     */
    public void setBackgroundColor(int color){
        mLayout.setBackgroundColor(color);
    }

    /**
     * {@link #setBackgroundColor(int)}
     *
     * @param color
     */
    public void setBackgroundColor(String color){
        setBackgroundColor(Color.parseColor(color));
    }

    /**
     * {@link #setBackgroundColor(int)}
     *
     * @param red
     * @param green
     * @param blue
     */
    public void setBackgroundColor(int red, int green, int blue){
        setBackgroundColor(Color.rgb(red, green, blue));
    }

    /**
     * {@link #setBackgroundColor(int)}
     *
     * @param alpha
     * @param red
     * @param green
     * @param blue
     */
    public void setBackgroundColor(int alpha, int red, int green, int blue){
        setBackgroundColor(Color.argb(alpha, red, green, blue));
    }

    public void setBackgroundAlpha(int alpha){
        setBackgroundColor(Color.argb(alpha, DEFAULT_RED, DEFAULT_GREEN, DEFAULT_BLUE));
    }

    /**
     * Set the title on actionbar.
     *
     * @param title The title on actionbar.
     */
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /**
     * @param title
     */
    public void setTitle(int title) {
        mTitle.setText(title);
    }

    /**
     * @return
     */
    public String getTitle() {
        return mTitle.getText().toString();
    }

    public void setBackIcon(int resId) {
        mBack.setImageResource(resId);
    }

    public void setMoreIcon(int resId) {
        mMore.setImageResource(resId);
    }

    public void setBackVisible(boolean visible) {
        setViewVisible(mBack, visible);
    }

    public void setTitleVisible(boolean visible) {
        setViewVisible(mTitle, visible);
    }

    public void setMoreVisible(boolean visible) {
        setViewVisible(mMore, visible);
    }

    private void setViewVisible(View target, boolean visible) {
        target.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    public int getActionBarHeight() {
        return this.ACTIONBAR_HEIGHT;
    }

    public void setBackView(View view) {
        replaceView(mBack, view, R.id.actionbar_back);
    }

    public ImageButton getBackView(){
        return this.mBack;
    }

    public void setTitleView(View view) {
        replaceView(mTitle, view, 0);
    }

    public TextView getTitleView(){
        return this.mTitle;
    }

    public void setMoreView(View view) {
        replaceView(mMore, view, R.id.actionbar_more);
    }

    public ImageButton getMoreView(){
        return this.mMore;
    }

    private void replaceView(View oldView, View newView, int newId) {
        mLayout.removeView(oldView);
        newView.setLayoutParams(oldView.getLayoutParams());
        int index = 0;
        if (oldView == mBack) {
            index = 0;
        } else if (oldView == mTitle) {
            index = 1;
        } else if (oldView == mMore) {
            index = 2;
        }
        mLayout.addView(newView, index);
        newView.setId(newId);
        newView.setOnClickListener(this);
    }

    public interface OnBackListener {
        void onBack(View v);
    }

    public interface OnMoreListener {
        void onMore(View v);
    }

    public void setOnBackListener(OnBackListener onBackListener) {
        this.onBackListener = onBackListener;
    }

    public void setOnMoreListener(OnMoreListener onMoreListener) {
        this.onMoreListener = onMoreListener;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.actionbar_back:

                if (this.onBackListener != null) {
                    this.onBackListener.onBack(v);
                } else {
                    ((Activity) getContext()).finish();
                }

                break;

            case R.id.actionbar_more:

                if (this.onMoreListener != null) {
                    this.onMoreListener.onMore(v);
                }

                break;
        }

    }

}
